home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_08 / phillip2 / warpsubs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-20  |  15.0 KB  |  605 lines

  1.  
  2.     /***********************************************
  3.     *
  4.     *    file d:\cips\warpsubs.c
  5.     *
  6.     *    Functions: This file contains
  7.     *       warp
  8.     *       warp_loop
  9.     *       bi_warp_loop
  10.     *       object_warp
  11.     *       full_warp_loop
  12.     *       bi_full_warp_loop
  13.     *
  14.     *    Purpose:
  15.     *       These functions performs different
  16.     *       geometric operations.
  17.     *
  18.     *    External Calls:
  19.     *       wtiff.c - round_off_image_size
  20.     *                 create_file_if_needed
  21.     *                 write_array_into_tiff_image
  22.     *       tiff.c - read_tiff_header
  23.     *       rtiff.c - read_tiff_image
  24.     *       geosubs.c - bilinear_interpolate
  25.     *
  26.     *    Modifications:
  27.     *       20 October 1993- created
  28.     *
  29.     *************************************************/
  30.  
  31. #include "cips.h"
  32.  
  33. #define FILL 150
  34.  
  35.  
  36.      /*******************************************
  37.      *
  38.      *   warp(..
  39.      *
  40.      *   This routine warps a ROWSxCOLS section
  41.      *   of an image.  The il, ie parameters
  42.      *   specify which ROWSxCOLS section of
  43.      *   the image to warp.  The x_control and
  44.      *   y_control parameters are the control
  45.      *   points inside that section.  Therefore,
  46.      *   x_control and y_control will always be
  47.      *   less the COLS and ROWS.
  48.      *
  49.      *   The point coordinates are for the four
  50.      *   corners of a four side figure.
  51.      *      x1,y1     x2,y2
  52.      *
  53.      *      x4,y4     x3,y3
  54.      *
  55.      *******************************************/
  56.  
  57. warp(in_name, out_name, the_image, out_image,
  58.      il, ie, ll, le, x_control, y_control,
  59.      bilinear)
  60.    char   in_name[], out_name[];
  61.    int    bilinear, il, ie, ll, le, 
  62.           x_control, y_control;
  63.    short  the_image[ROWS][COLS],
  64.           out_image[ROWS][COLS];
  65. {
  66.    int    cols_div_2, extra_x, extra_y, i, j,
  67.           rows_div_2, x1, x2, x3, x4, y1, y2, y3, y4;
  68.  
  69.    struct tiff_header_struct image_header;
  70.  
  71.    create_file_if_needed(in_name, out_name, out_image);
  72.  
  73.    read_tiff_image(in_name, the_image, il, ie, ll, le);
  74.  
  75.    cols_div_2 = COLS/2;
  76.    rows_div_2 = ROWS/2;
  77.  
  78.       /***********************************
  79.       *
  80.       *   1 - upper left quarter
  81.       *
  82.       ***********************************/
  83.  
  84.    x1 = 0;
  85.    x2 = cols_div_2;
  86.    x3 = x_control;
  87.    x4 = 0;
  88.  
  89.    y1 = 0;
  90.    y2 = 0;
  91.    y3 = y_control;
  92.    y4 = rows_div_2;
  93.  
  94.    extra_x = 0;
  95.    extra_y = 0;
  96.  
  97.    if(bilinear)
  98.       bi_warp_loop(the_image, out_image,
  99.                    x1, x2, x3, x4,
  100.                    y1, y2, y3, y4,
  101.                    extra_x, extra_y);
  102.    else
  103.       warp_loop(the_image, out_image,
  104.                 x1, x2, x3, x4,
  105.                 y1, y2, y3, y4,
  106.                 extra_x, extra_y);
  107.  
  108.       /***********************************
  109.       *
  110.       *   2 - upper right quarter
  111.       *
  112.       ***********************************/
  113.  
  114.    x1 = cols_div_2;
  115.    x2 = COLS-1;
  116.    x3 = COLS-1;
  117.    x4 = x_control;
  118.  
  119.    y1 = 0;
  120.    y2 = 0;
  121.    y3 = rows_div_2;
  122.    y4 = y_control;
  123.  
  124.    extra_x = cols_div_2;
  125.    extra_y = 0;
  126.  
  127.  
  128.    if(bilinear)
  129.       bi_warp_loop(the_image, out_image,
  130.                    x1, x2, x3, x4,
  131.                    y1, y2, y3, y4,
  132.                    extra_x, extra_y);
  133.    else
  134.       warp_loop(the_image, out_image,
  135.                 x1, x2, x3, x4,
  136.                 y1, y2, y3, y4,
  137.                 extra_x, extra_y);
  138.  
  139.       /***********************************
  140.       *
  141.       *   3 - lower right quarter
  142.       *
  143.       ***********************************/
  144.  
  145.    x1 = x_control;
  146.    x2 = COLS-1;
  147.    x3 = COLS-1;
  148.    x4 = cols_div_2;
  149.  
  150.    y1 = y_control;
  151.    y2 = rows_div_2;
  152.    y3 = ROWS-1;
  153.    y4 = ROWS-1;
  154.  
  155.    extra_x = cols_div_2;
  156.    extra_y = rows_div_2;
  157.  
  158.    if(bilinear)
  159.       bi_warp_loop(the_image, out_image,
  160.                    x1, x2, x3, x4,
  161.                    y1, y2, y3, y4,
  162.                    extra_x, extra_y);
  163.    else
  164.       warp_loop(the_image, out_image,
  165.                 x1, x2, x3, x4,
  166.                 y1, y2, y3, y4,
  167.                 extra_x, extra_y);
  168.  
  169.       /***********************************
  170.       *
  171.       *   4 - lower left quarter
  172.       *
  173.       ***********************************/
  174.  
  175.    x1 = 0;
  176.    x2 = x_control;
  177.    x3 = cols_div_2;
  178.    x4 = 0;
  179.  
  180.    y1 = rows_div_2;
  181.    y2 = y_control;
  182.    y3 = ROWS-1;
  183.    y4 = ROWS-1;
  184.  
  185.    extra_x = 0;
  186.    extra_y = rows_div_2;
  187.  
  188.    if(bilinear)
  189.       bi_warp_loop(the_image, out_image,
  190.                    x1, x2, x3, x4,
  191.                    y1, y2, y3, y4,
  192.                    extra_x, extra_y);
  193.    else
  194.       warp_loop(the_image, out_image,
  195.                 x1, x2, x3, x4,
  196.                 y1, y2, y3, y4,
  197.                 extra_x, extra_y);
  198.  
  199.    write_array_into_tiff_image(out_name, out_image,
  200.                                il, ie, ll, le);
  201. }  /* ends warp */
  202.  
  203.  
  204.  
  205.  
  206.  
  207.      /*******************************************
  208.      *
  209.      *   warp_loop(..
  210.      *
  211.      *   This routine sets up the coefficients
  212.      *   and loops through a quarter of the
  213.      *   ROWSxCOLS section of the image that
  214.      *   is being warped.
  215.      *
  216.      *******************************************/
  217.  
  218. warp_loop(the_image, out_image,
  219.           x1, x2, x3, x4,
  220.           y1, y2, y3, y4,
  221.           extra_x, extra_y)
  222.    int    extra_x, extra_y,
  223.           x1, x2, x3, x4,
  224.           y1, y2, y3, y4;
  225.    short  the_image[ROWS][COLS],
  226.           out_image[ROWS][COLS];
  227. {
  228.    int    cols_div_2, denom, i, j, rows_div_2,
  229.           xa, xb, xab, x_out, ya, yb, yab, y_out;
  230.  
  231.    cols_div_2 = COLS/2;
  232.    rows_div_2 = ROWS/2;
  233.    denom      = cols_div_2 * rows_div_2;
  234.  
  235.       /***********************************
  236.      *
  237.      *   Set up the terms for the
  238.      *   spatial transformation.
  239.      *
  240.      ***********************************/
  241.  
  242.    xa  = x2 - x1;
  243.    xb  = x4 - x1;
  244.    xab = x1 - x2 + x3 - x4;
  245.  
  246.    ya  = y2 - y1;
  247.    yb  = y4 - y1;
  248.    yab = y1 - y2 + y3 - y4;
  249.  
  250.       /***********************************
  251.      *
  252.      *   Loop through a quadrant and
  253.      *   perform the spatial
  254.      *   transformation.
  255.      *
  256.      ***********************************/
  257.  
  258.       /* NOTE a=j b=i */
  259.  
  260.    printf("\n");
  261.    for(i=0; i<rows_div_2; i++){
  262.       if( (i%10) == 0) printf("%d ", i);
  263.       for(j=0; j<cols_div_2; j++){
  264.  
  265.          x_out = x1 + (xa*j)/cols_div_2 +
  266.                (xb*i)/rows_div_2 + (xab*i*j)/(denom);
  267.          y_out = y1 + (ya*j)/cols_div_2 +
  268.                (yb*i)/rows_div_2 + (yab*i*j)/(denom);
  269.  
  270.          if(x_out < 0       ||
  271.             x_out >= COLS   ||
  272.             y_out < 0       ||
  273.             y_out >= ROWS)
  274.             out_image[i+extra_y][j+extra_x] = FILL;
  275.          else
  276.             out_image[i+extra_y][j+extra_x] =
  277.              the_image[y_out][x_out];
  278.  
  279.       }  /* ends loop over j */
  280.    }  /* ends loop over i */
  281.  
  282. }   /* ends warp_loop */
  283.  
  284.  
  285.  
  286.  
  287.  
  288.      /*******************************************
  289.      *
  290.      *   bi_warp_loop(..
  291.      *
  292.      *   This routine sets up the coefficients
  293.      *   and loops through a quarter of the
  294.      *   ROWSxCOLS section of the image that
  295.      *   is being warped.
  296.      *
  297.      *   This version of the routine uses bilinear
  298.      *   interpolation to find the gray shades.
  299.      *   It is more accurate than warp_loop,
  300.      *   but takes longer because of the floating
  301.      *   point calculations.
  302.      *
  303.      *******************************************/
  304.  
  305. bi_warp_loop(the_image, out_image,
  306.              x1, x2, x3, x4,
  307.              y1, y2, y3, y4,
  308.              extra_x, extra_y)
  309.    int    extra_x, extra_y,
  310.           x1, x2, x3, x4,
  311.           y1, y2, y3, y4;
  312.    short  the_image[ROWS][COLS],
  313.           out_image[ROWS][COLS];
  314. {
  315.    double cols_div_2, denom, di, dj, rows_div_2,
  316.           xa, xb, xab, x_out, ya, yb, yab, y_out;
  317.    int    i, j;
  318.  
  319.    cols_div_2 = (double)(COLS)/2.0;
  320.    rows_div_2 = (double)(ROWS)/2.0;
  321.    denom      = cols_div_2 * rows_div_2;
  322.  
  323.      /***********************************
  324.      *
  325.      *   Set up the terms for the
  326.      *   spatial transformation.
  327.      *
  328.      ***********************************/
  329.  
  330.    xa  = x2 - x1;
  331.    xb  = x4 - x1;
  332.    xab = x1 - x2 + x3 - x4;
  333.  
  334.    ya  = y2 - y1;
  335.    yb  = y4 - y1;
  336.    yab = y1 - y2 + y3 - y4;
  337.  
  338.      /***********************************
  339.      *
  340.      *   Loop through a quadrant and
  341.      *   perform the spatial
  342.      *   transformation.
  343.      *
  344.      ***********************************/
  345.  
  346.       /* NOTE a=j b=i */
  347.  
  348.    printf("\n");
  349.    for(i=0; i<rows_div_2; i++){
  350.       if( (i%10) == 0) printf("%d ", i);
  351.       for(j=0; j<cols_div_2; j++){
  352.  
  353.        di = (double)(i);
  354.        dj = (double)(j);
  355.  
  356.          x_out = x1 +
  357.                  (xa*dj)/cols_div_2 +
  358.                  (xb*di)/rows_div_2 +
  359.                  (xab*di*dj)/(denom);
  360.          y_out = y1 +
  361.                  (ya*dj)/cols_div_2 +
  362.                  (yb*di)/rows_div_2 +
  363.                  (yab*di*dj)/(denom);
  364.  
  365.          out_image[i+extra_y][j+extra_x] =
  366.           bilinear_interpolate(the_image, x_out, y_out);
  367.  
  368.       }  /* ends loop over j */
  369.    }  /* ends loop over i */
  370.  
  371. }   /* ends bi_warp_loop */
  372.  
  373.  
  374.  
  375.  
  376.  
  377.      /*******************************************
  378.      *
  379.      *   object_warp(..
  380.      *
  381.      *   This routine warps a ROWSxCOLS section
  382.      *   of an image.  The il, ie parameters
  383.      *   specify which ROWSxCOLS section of
  384.      *   the image to warp.  The x_control and
  385.      *   y_control parameters are the control
  386.      *   points inside that section.  Therefore,
  387.      *   x_control and y_control will always be
  388.      *   less the COLS and ROWS.
  389.      *
  390.      *   The point coordinates are for the four
  391.      *   corners of a four side figure.
  392.      *      x1,y1     x2,y2
  393.      *
  394.      *      x4,y4     x3,y3
  395.      *
  396.      *******************************************/
  397.  
  398. object_warp(in_name, out_name, 
  399.             the_image, out_image,
  400.             il, ie, ll, le, 
  401.             x1, y1, x2, y2, x3, y3, x4, y4, 
  402.             bilinear)
  403.    char   in_name[], out_name[];
  404.    int    bilinear, il, ie, ll, le, 
  405.           x1, y1, x2, y2, x3, y3, x4, y4;
  406.    short  the_image[ROWS][COLS],
  407.           out_image[ROWS][COLS];
  408. {
  409.    int    extra_x = 0, 
  410.           extra_y = 0;
  411.  
  412.    struct tiff_header_struct image_header;
  413.  
  414.    create_file_if_needed(in_name, out_name, out_image);
  415.  
  416.    read_tiff_image(in_name, the_image, il, ie, ll, le);
  417.  
  418.       /***********************************
  419.       *
  420.       *   Call the warp loop function you
  421.       *   need (with or without bilinear
  422.       *   interpolation).
  423.       *
  424.       ***********************************/
  425.  
  426.    if(bilinear)
  427.       bi_full_warp_loop(the_image, out_image,
  428.                         x1, x2, x3, x4,
  429.                         y1, y2, y3, y4,
  430.                         extra_x, extra_y);
  431.    else
  432.       full_warp_loop(the_image, out_image,
  433.                      x1, x2, x3, x4,
  434.                      y1, y2, y3, y4,
  435.                      extra_x, extra_y);
  436.  
  437.    write_array_into_tiff_image(out_name, out_image,
  438.                                il, ie, ll, le);
  439. }  /* ends object_warp */
  440.  
  441.  
  442.  
  443.  
  444.      /*******************************************
  445.      *
  446.      *   full_warp_loop(..
  447.      *
  448.      *   This routine sets up the coefficients
  449.      *   and loops through an entire
  450.      *   ROWSxCOLS image that is being warped.
  451.      *
  452.      *******************************************/
  453.  
  454. full_warp_loop(the_image, out_image,
  455.                x1, x2, x3, x4,
  456.                y1, y2, y3, y4,
  457.                extra_x, extra_y)
  458.    int    extra_x, extra_y,
  459.           x1, x2, x3, x4,
  460.           y1, y2, y3, y4;
  461.    short  the_image[ROWS][COLS],
  462.           out_image[ROWS][COLS];
  463. {
  464.    int    cols_div_2, denom, i, j, rows_div_2,
  465.           xa, xb, xab, x_out, ya, yb, yab, y_out;
  466.  
  467.    cols_div_2 = COLS/2;
  468.    rows_div_2 = ROWS/2;
  469.    denom      = cols_div_2 * rows_div_2;
  470.  
  471.       /***********************************
  472.      *
  473.      *   Set up the terms for the
  474.      *   spatial transformation.
  475.      *
  476.      ***********************************/
  477.  
  478.    xa  = x2 - x1;
  479.    xb  = x4 - x1;
  480.    xab = x1 - x2 + x3 - x4;
  481.  
  482.    ya  = y2 - y1;
  483.    yb  = y4 - y1;
  484.    yab = y1 - y2 + y3 - y4;
  485.  
  486.       /***********************************
  487.      *
  488.      *   Loop through the image and
  489.      *   perform the spatial
  490.      *   transformation.
  491.      *
  492.      ***********************************/
  493.  
  494.       /* NOTE a=j b=i */
  495.  
  496.    printf("\n");
  497.    for(i=0; i<ROWS; i++){
  498.       if( (i%10) == 0) printf("%d ", i);
  499.       for(j=0; j<COLS; j++){
  500.  
  501.          x_out = x1 + (xa*j)/COLS +
  502.                (xb*i)/ROWS + (xab*i*j)/(denom);
  503.          y_out = y1 + (ya*j)/COLS +
  504.                (yb*i)/ROWS + (yab*i*j)/(denom);
  505.  
  506.          if(x_out < 0       ||
  507.             x_out >= COLS   ||
  508.             y_out < 0       ||
  509.             y_out >= ROWS)
  510.             out_image[i+extra_y][j+extra_x] = FILL;
  511.          else
  512.             out_image[i+extra_y][j+extra_x] =
  513.              the_image[y_out][x_out];
  514.  
  515.       }  /* ends loop over j */
  516.    }  /* ends loop over i */
  517.  
  518. }   /* ends full_warp_loop */
  519.  
  520.  
  521.  
  522.  
  523.  
  524.      /*******************************************
  525.      *
  526.      *   bi_full_warp_loop(..
  527.      *
  528.      *   This routine sets up the coefficients
  529.      *   and loops through an entire
  530.      *   ROWSxCOLS image that is being warped.
  531.      *
  532.      *   This version of the routine uses bilinear
  533.      *   interpolation to find the gray shades.
  534.      *   It is more accurate than warp_loop,
  535.      *   but takes longer because of the floating
  536.      *   point calculations.
  537.      *
  538.      *******************************************/
  539.  
  540. bi_full_warp_loop(the_image, out_image,
  541.                   x1, x2, x3, x4,
  542.                   y1, y2, y3, y4,
  543.                   extra_x, extra_y)
  544.    int    extra_x, extra_y,
  545.           x1, x2, x3, x4,
  546.           y1, y2, y3, y4;
  547.    short  the_image[ROWS][COLS],
  548.           out_image[ROWS][COLS];
  549. {
  550.    double denom, di, dj,
  551.           xa, xb, xab, x_out, ya, yb, yab, y_out;
  552.    int    i, j;
  553.  
  554.    denom      = COLS * ROWS;
  555.  
  556.      /***********************************
  557.      *
  558.      *   Set up the terms for the
  559.      *   spatial transformation.
  560.      *
  561.      ***********************************/
  562.  
  563.    xa  = x2 - x1;
  564.    xb  = x4 - x1;
  565.    xab = x1 - x2 + x3 - x4;
  566.  
  567.    ya  = y2 - y1;
  568.    yb  = y4 - y1;
  569.    yab = y1 - y2 + y3 - y4;
  570.  
  571.      /***********************************
  572.      *
  573.      *   Loop through the image and
  574.      *   perform the spatial
  575.      *   transformation.
  576.      *
  577.      ***********************************/
  578.  
  579.       /* NOTE a=j b=i */
  580.  
  581.    printf("\n");
  582.    for(i=0; i<ROWS; i++){
  583.       if( (i%10) == 0) printf("%d ", i);
  584.       for(j=0; j<COLS; j++){
  585.  
  586.          di = (double)(i);
  587.          dj = (double)(j);
  588.  
  589.          x_out = x1 +
  590.                  (xa*dj)/COLS +
  591.                  (xb*di)/ROWS +
  592.                  (xab*di*dj)/(denom);
  593.          y_out = y1 +
  594.                  (ya*dj)/COLS +
  595.                  (yb*di)/ROWS +
  596.                  (yab*di*dj)/(denom);
  597.  
  598.          out_image[i+extra_y][j+extra_x] =
  599.           bilinear_interpolate(the_image, x_out, y_out);
  600.  
  601.       }  /* ends loop over j */
  602.    }  /* ends loop over i */
  603.  
  604. }   /* ends bi_full_warp_loop */
  605.